package cn.gp.chat.controller;

import cn.gp.chat.commom.res.AddFriendRequest;
import cn.gp.chat.commom.res.LoginRequest;
import cn.gp.chat.commom.res.RegisterRequest;
import cn.gp.chat.commom.res.UpdateProfileRequest;
import cn.gp.chat.commom.resp.FriendRequestVo;
import cn.gp.chat.commom.resp.UserVo;
import cn.gp.chat.common.utils.ApiResponse;
import cn.gp.chat.service.UserService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/user")
@RequiredArgsConstructor
@Slf4j
public class UserController {
    // 使用final字段，利用lombok的@RequiredArgsConstructor实现注入
    private final UserService userService;

    /**
     * @Param request
     * @Return cn.gp.chat.common.utils.ApiResponse<cn.gp.chat.commom.resp.UserVo>
     * @Description 用户登录
     * @Author He Guoping
     * @Date 2025/6/19 9:46
     */
    @PostMapping("/login")
    public ApiResponse<UserVo> login(@RequestBody LoginRequest request) {
        UserVo userVo = userService.login(request);
        return ApiResponse.success(userVo);
    }

    /**
     * @Param request
     * @Return cn.gp.chat.common.utils.ApiResponse<cn.gp.chat.commom.resp.UserVo>
     * @Description 用户注册
     * @Author He Guoping
     * @Date 2025/6/19 9:46
     */
    @PostMapping("/register")
    public ApiResponse<UserVo> register(@RequestBody RegisterRequest request) {
        UserVo userVo = userService.register(request);
        return ApiResponse.success(userVo);
    }

    /**
     * @Param request
     * @Return cn.gp.chat.common.utils.ApiResponse<cn.gp.chat.commom.resp.UserVo>
     * @Description 修改个人信息
     * @Author He Guoping
     * @Date 2025/6/19 9:47
     */
    @PutMapping("/profile")
    public ApiResponse<UserVo> updateProfile(@RequestBody UpdateProfileRequest request) {
        UserVo userVo = userService.updateProfile(request);
        return ApiResponse.success(userVo);
    }

    /**
     * @Param keyword
     * @Return cn.gp.chat.common.utils.ApiResponse<java.util.List < cn.gp.chat.commom.resp.UserVo>>
     * @Description 搜索好友
     * @Author He Guoping
     * @Date 2025/6/19 9:47
     */
    @GetMapping("/search")
    public ApiResponse<List<UserVo>> searchUsers(@RequestParam String keyword) {
        List<UserVo> users = userService.searchUsers(keyword);
        return ApiResponse.success(users);
    }

    /**
     * @Param request
     * @Return cn.gp.chat.common.utils.ApiResponse<java.lang.Void>
     * @Description 发送好友申请
     * @Author He Guoping
     * @Date 2025/6/19 9:47
     */
    @PostMapping("/friend/request")
    public ApiResponse<Void> sendFriendRequest(@RequestBody AddFriendRequest request) {
        log.info("/friend/request:" + request.getRequesterId());
        log.info("/friend/request:" + request.getReceiverId());
        log.info("/friend/request:" + request.getRequestMessage());
        userService.sendFriendRequest(request.getRequesterId(), request.getReceiverId(), request.getRequestMessage());
        return ApiResponse.success();
    }

    /**
     * @Param userId
     * @Return cn.gp.chat.common.utils.ApiResponse<java.util.List < cn.gp.chat.commom.resp.FriendRequestVo>>
     * @Description 获取好友申请
     * @Author He Guoping
     * @Date 2025/6/19 9:48
     */
    @GetMapping("/friend/requests")
    public ApiResponse<List<FriendRequestVo>> getFriendRequests(@RequestParam Long userId) {
        log.info("/friend/requests:" + userId);
        List<FriendRequestVo> requests = userService.getFriendRequests(userId);
        return ApiResponse.success(requests);
    }

    /**
     * @Param requestId
     * @Param status
     * @Param userId
     * @Return cn.gp.chat.common.utils.ApiResponse<java.lang.Void>
     * @Description 处理好友请求
     * @Author He Guoping
     * @Date 2025/6/19 9:48
     */
    @PutMapping("/friend/request/{requestId}")
    public ApiResponse<Void> handleFriendRequest(@PathVariable Long requestId, 
                                               @RequestParam Integer status,
                                               @RequestParam Long userId) {
        log.info("/friend/request/{}:", requestId);
        log.info("/friend/request/{},status={},userId={}", requestId, status, userId);
        userService.handleFriendRequest(userId, requestId, status);
        return ApiResponse.success();
    }

    /**
     * @Param userId
     * @Return cn.gp.chat.common.utils.ApiResponse<cn.gp.chat.commom.resp.UserVo>
     * @Description 获取当前用户信息
     * @Author He Guoping
     * @Date 2025/6/19 9:49
     */
    @GetMapping("/current")
    public ApiResponse<UserVo> getCurrentUser(@RequestParam Long userId) {
        log.info("/current:{}", userId);
        UserVo userVo = userService.getCurrentUser(userId);
        return ApiResponse.success(userVo);
    }
}